import { ref, shallowRef } from 'vue'

export default <D = any, E = any>(handler: () => Promise<D>) => {
  const data = shallowRef<D>()
  const error = shallowRef<E>()
  const loading = ref(true)

  handler()
    .then((d) => (data.value = d))
    .catch((e) => (error.value = e))
    .finally(() => (loading.value = false))

  return {
    data,
    error,
    loading
  }
}
